<template>
  <div class="employees">
    <!-- 工具栏 -->
    <mytool :left-show="false">
      <template #right>
        <el-button
          :disabled="'employees_import' | isInPoint"
          type="primary"
          size="mini"
          @click="$router.push('/employees/import')"
        >批量导入员工信息</el-button>
        <el-button
          :disabled="'employees_add' | isInPoint"
          type="primary"
          size="mini"
          @click="addOpen"
        ><i class="el-icon-plus" />新增员工</el-button>
      </template>
    </mytool>
    <!-- 内容区域 -->
    <el-card class="mycard">
      <!-- 内 - 表格 -->
      <el-table :data="list" border>
        <el-table-column label="序号" type="index" width="60px" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <img
              v-imgerror="defaultimg"
              :src="scope.row.staffPhoto"
              alt=""
              class="myavatar"
              @click="openPre(scope.row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <template slot-scope="scope">
            {{ scope.row.formOfEmployment | trunword }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template slot-scope="scope">
            {{ scope.row.timeOfEntry | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="270px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="'employees_check' | isInPoint"
              type="text"
              @click="$router.push(`/employees/detail?id=${scope.row.id}`)"
            >查看</el-button>
            <el-button
              type="text"
              :disabled="'employees_turn' | isInPoint"
            >转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="openRole(scope.row)">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 内 - 分页 -->
      <el-pagination
        :current-page="page"
        :page-sizes="pagesizes"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <mypanel :dialog-show.sync="dialogShow" />
    <!-- 预览框 -->
    <el-dialog title="预览图片" :visible.sync="perShow" @close="guanbi">
      <!-- <img :src="perImg"> -->
      <canvas ref="myCanvas" />
    </el-dialog>
    <!-- 分配角色 -->
    <el-dialog title="分配角色" :visible.sync="roleShow">
      <!-- 角色区域 -->
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入qrcode插件
import QRcode from 'qrcode'
import mypanel from './component/mypanel.vue'
import {
  getUserListAPI,
  getUserInfoAPI,
  setRoleAPI,
  getRoleListAPI
} from '@/api/index'
export default {
  components: {
    mypanel
  },
  data() {
    return {
      list: [], // 表格的数据源
      page: 1, // 当前页
      pagesizes: [5, 10, 15, 20], // 页容量集合
      pagesize: 5, // 页容量
      total: 0, // 数据的总条数
      dialogShow: false,
      // 添加一张默认图片
      defaultimg: require('@/assets/微信头像.png'),
      // 控制预览面板的显示与隐藏
      perShow: false,
      // 预览的图片地址
      perImg: '',
      // 控制分配角色面板的显示与隐藏
      roleShow: false,
      // 角色多选的数组
      checkList: [],
      // 定义一个当前用户操作的id
      userId: '',
      // 定义一个角色的集合
      roleList: []
    }
  },
  created() {
    // 打开页面时发起请求得到数据
    this.getUserList()
    this.getRoleList()
  },
  methods: {
    // 得到所有的角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data.rows
    },
    // 点击提交修改后的角色信息
    async submit() {
      const res = await setRoleAPI({
        id: this.userId, // 当前操作用户的id
        roleIds: this.checkList // 选择的角色定位
      })
      // 提交后的提示信息
      this.$message.success(res.message)
      // 关闭面板
      this.cancel()
    },
    // 点击关闭面板
    cancel() {
      this.roleShow = false
    },
    // 打开角色面板
    async openRole(row) {
      // 保存当前操作的用户的id
      this.userId = row.id
      this.roleShow = true
      // 得到当前点击用户的基本信息
      const res = await getUserInfoAPI(row.id)
      // 保存当前点击用户的角色信息
      this.checkList = res.data.roleIds
    },
    // 关闭对话框时触发
    guanbi() {
      // 隐藏对话框
      this.perShow = false
      // 清除图片路径
      this.perImg = ''
    },
    // 打开预览面板
    openPre(img) {
      // QRcode.toCanvas(dom(需要显示二维码的dom), info(要生成二维码的地址),{配制(width,height)})  // qrcode的配置信息
      this.perShow = true
      // 保存图片地址
      this.perImg = img
      this.$nextTick(() => {
        this.$message.success('请扫描二维码预览图片')
        // 转换图片地址为二维码
        QRcode.toCanvas(
          // 显示二维码
          this.$refs.myCanvas,
          img, // 要转换的地址
          {
            width: 220,
            height: 220
          } // 配置信息
        )
      })
    },
    addOpen() {
      this.dialogShow = true
    },
    // 获取员工列表数据
    async getUserList() {
      const res = await getUserListAPI({
        page: this.page,
        size: this.pagesize
      })
      // 保存返回的数据
      this.list = res.data.rows
      // 保存总数据
      this.total = res.data.total
    },
    // 当页容量改变时会触发
    handleSizeChange(size) {
      // 修改页容量
      this.pagesize = size
      // 重新请求数据
      this.getUserList()
    },
    // 当前页改变时会触发
    handleCurrentChange(page) {
      // 重新渲染页面
      this.page = page
      this.getUserList()
    }
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .el-pagination {
    text-align: right;
    margin-top: 20px;
  }
  .mycard {
    margin-top: 20px;
  }
  .myavatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
  }
  .el-dialog {
    width: 38%;
    text-align: center;
    img {
      max-width: 82%;
    }
  }
}
</style>
